<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        Function.prototype.myBind = function(context) {
            if (typeof this !== 'function') {
                throw new TypeError('Error')
            }

            context = context || window
            const _this = this
            const args = Array.slice.call(arguments, 1)

            return function F() {
                // 使用 new
                if (this instanceof F) {
                    return new _this(...args, ...arguments)
                }
                return _this.call(context, ...args, ...arguments)
            }
        }

        const obj = {
            name: 'jlq'
        }
        function sayName() {
            console.log(this.name);
        }
        const fn = sayName.bind(obj)
        fn()
    </script>
</body>
</html>